const mongoose = require('mongoose')
const handleE11000 = require('../common/error-handle.api').handleE11000
const Schema = mongoose.Schema
const UserSchemaConfig = {
  versionKey: false,
  timestamps: {
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  },
  runSettersOnQuery: true
}

const UserSchema = new Schema({
  arancia: { type: String, required: true },
  username: { type: String, minlength: 2, maxlength: 20, required: true },
  phone: {
    type: String,
    validate: {
      validator: function(value) {
        return /^1\d{10}$/.test(value)
      },
      message: '{VALUE} is not a valid phone number'
    },
    required: [true, 'User phone number required'],
    unique: true
  },
  password: { type: String, required: true },
  email: { type: String, lowercase: true, default: '' },
  class: { type: String, default: '' },
  cadre: { type: String, default: '' },
  qq: { type: String, default: '' },
  wechat: { type: String, default: '' },
  interests: [String],
  manager: { type: Boolean, default: false },
  contrast: { type: Schema.Types.ObjectId, ref: 'User' },
  followers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
  dormitory: { type: String, default: '' },
  room: { type: String, default: '' },
  bed: { type: String, default: '' },
  avatar: Buffer,
  archive: {
    name: String,
    sex: { type: String, maxlength: 1 },
    nation: String,
    politics_status: String,
    examinee_type: String,
    senior_high_school: String,
    university: String,
    campus: String,
    school: String,
    major: String,
    others: { type: String, default: '' },
    school_roll_num: {
      type: String,
      validate: {
        validator: function(value) {
          return /^\d+$/.test(value)
        },
        message: 'roll number only allow numbers'
      }
    },
    identity_card_num: {
      type: String,
      validate: function(value) {
        return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(value)
      },
      message: '18-char-long valid identity_card is required'
    },
    birthdate: String,
    province: String,
    county: String,
    contact: String,
    family_information: String,
    father: String,
    father_contact: String,
    mother: String,
    mother_contact: String,
    address: String,
    postcode: String,
    addressee: String
  },
  teams: [{
    team: { type: Schema.Types.ObjectId, ref: 'Team' },
    creator: { type: Boolean, default: false },
    management: {
      announcement: { type: Boolean, default: false },
      resource: { type: Boolean, default: false },
      vote: { type: Boolean, default: false },
      registry: { type: Boolean, default: false },
      party: { type: Boolean, default: false },
      evaluation: { type: Boolean, default: false },
      bursary: { type: Boolean, default: false },
      dormitory: { type: Boolean, default: false },
      contrast: { type: Boolean, default: false }
    },
    management_tags: [{ type: Schema.Types.ObjectId, ref: 'ManagementTag' }]
  }],
  current_party_stage: { type: Number, default: 0 }
}, UserSchemaConfig)

UserSchema.index({ phone: 1 })
UserSchema.post('save', handleE11000)
UserSchema.post('update', handleE11000)
UserSchema.post('findOneAndUpdate', handleE11000)
UserSchema.post('insertMany', handleE11000)

exports = module.exports = UserSchema
